package com.dmooo.yxmmerchant.view.ui;

import android.view.LayoutInflater;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.common.dialog.MyDialog;
import com.common.net.entity.SubjectPost;
import com.common.net.listener.HttpOnNextListener;
import com.common.net.subscribers.ProgressSubscriber;
import com.common.util.ToastUtil;
import com.dmooo.yxmmerchant.R;
import com.dmooo.yxmmerchant.base.BaseActivity;
import com.dmooo.yxmmerchant.http.HttpManager;
import com.dmooo.yxmmerchant.util.DrawableCenterTextView;
import com.google.gson.JsonArray;
import com.google.gson.JsonObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.OnClick;

public class SetPartialActivity extends BaseActivity {
    @BindView(R.id.tv_left)
    DrawableCenterTextView tvLeft;
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.txt_order_num)
    TextView txtOrderNum;
    @BindView(R.id.txt_name)
    TextView txtName;
    @BindView(R.id.txt_sku)
    TextView txtSku;
    @BindView(R.id.txt_user_name)
    TextView txtUserName;
    @BindView(R.id.txt_phone)
    TextView txtPhone;
    @BindView(R.id.ll_add_partial)
    LinearLayout llAddPartial;
    @BindView(R.id.et_money)
    EditText etMoney;
    @BindView(R.id.et_day)
    EditText etDay;
    private List<EditText> moneys=new ArrayList<>();
    private List<EditText> days=new ArrayList<>();
    @Override
    protected int getLayout() {
        return R.layout.activity_set_partial;
    }

    @Override
    protected void initEventAndData() {
        tvTitle.setText("支付设置");
        moneys.add(etMoney);
        days.add(etDay);
        txtOrderNum.setText(getIntent().getExtras().getString("order_num"));
        txtName.setText(getIntent().getExtras().getString("price"));
        txtSku.setText(getIntent().getExtras().getString("goods_name"));
        txtUserName.setText(getIntent().getExtras().getString("username"));
        txtPhone.setText(getIntent().getExtras().getString("phone"));
    }

    @Override
    protected void createPresenter() {

    }

    @OnClick({R.id.tv_left, R.id.txt_add,R.id.btn_ok})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.tv_left:
                finish();
                break;
            case R.id.txt_add:
                LinearLayout layout = (LinearLayout) LayoutInflater.from(this).inflate(R.layout.layout_set_partial, null);
                EditText et=layout.findViewById(R.id.et_money);
                moneys.add(et);
                EditText et2=layout.findViewById(R.id.et_day);
                days.add(et2);
                llAddPartial.addView(layout);
                ImageView imageView=layout.findViewById(R.id.icon_del);
                imageView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        moneys.remove(et);
                        days.remove(et2);
                        llAddPartial.removeView(layout);
                    }
                });
                break;
            case R.id.btn_ok:
                for(int i=0;i<moneys.size();i++){
                    if(moneys.get(i).getText().toString().equals("")||days.get(i).getText().toString().equals("")){
                        ToastUtil.getInstance().showErrorMsg("请完成全部分阶段信息");
                        return;
                    }
                }
                MyDialog.showMaterialDialog(getSupportFragmentManager(), "提示", "将要创建与买家协商的分阶段支付方式?", "再想想", new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                    }
                }, "确定提交", new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        submit();
                    }
                }).show();
                break;
        }
    }
    private void submit(){
//        token	char(32)	是	商户身份令牌
//        order_detail_id	int(10)	是	订单详情ID
//        partial_pay	json	是	分阶段支付配置
//        partial_pay->money:金额，按比例设置
//        partial_pay->day:间隔多少天后支付下一笔
        Map<String,Object> map=new HashMap<>();
        map.put("token",token);
        map.put("order_detail_id",getIntent().getExtras().getString("order_detail_id"));
        JsonArray array=new JsonArray();
        JsonObject obj;
        for(int i=0;i<moneys.size();i++){
            obj=new JsonObject();
            obj.addProperty("money",Double.valueOf(moneys.get(i).getText().toString())/100);
            obj.addProperty("day",days.get(i).getText().toString());
            array.add(obj);
        }
        map.put("partial_pay",array.toString());
        SubjectPost post=new SubjectPost(new ProgressSubscriber(new HttpOnNextListener() {
            @Override
            public void onNext(Object o) {
                ToastUtil.getInstance().showSuccessMsg("设置成功");
            finish();
            }

            @Override
            public void onFail(Object o) {
                ToastUtil.getInstance().showSuccessMsg(o.toString());
            }

            @Override
            public void onStart() {

            }

            @Override
            public void onComplete() {

            }
        }, this, false, false, "登录中"),map);
        HttpManager.getInstance().setPartialPay(post);

    }
}
